//
//  UploadImgCollectionViewCell.m
//  IntegralWall
//
//  Created by 小焘无量 on 2022/8/12.
//

#import "UploadImgCollectionViewCell.h"

@interface UploadImgCollectionViewCell()

@property (nonatomic, strong) UIImageView *contentImgView;
@property (nonatomic, strong) UIButton *deleteBtn;

@end


@implementation UploadImgCollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self UIConfig];
    }
    return self;
}

-(void) UIConfig {
    [self.contentView addSubview:self.contentImgView];
    [self.contentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.contentView addSubview:self.deleteBtn];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
}

-(void)deleteBtnAction {
    if (self.deleteImgBlock && self.img) {
        self.deleteImgBlock(self.img);
    }
    
    if (self.deleteImgUrlBlock && self.imgUrl) {
        self.deleteImgUrlBlock(self.imgUrl);
    }
    
    if (self.deleteAssetModelBlock && self.assetModel) {
        self.deleteAssetModelBlock(self.assetModel);
    }
}

-(void)setImg:(UIImage *)img {
    _img = img;
    self.contentImgView.image = img;
}

-(void)setImgUrl:(NSString *)imgUrl {
    _imgUrl = imgUrl;
    
    [_contentImgView yt_setImageURL:imgUrl];
}

-(void)setHidDeleteBtn:(BOOL)hidDeleteBtn {
    _hidDeleteBtn = hidDeleteBtn;
    self.deleteBtn.hidden = hidDeleteBtn;
}

-(void)setAssetModel:(TZAssetModel *)assetModel {
    _assetModel = assetModel;
}

#pragma mark -lazy
-(UIImageView *)contentImgView {
    if (!_contentImgView) {
        _contentImgView = [UIImageView new];
        _contentImgView.layer.cornerRadius = 6;
        _contentImgView.clipsToBounds = YES;
        _contentImgView.contentMode = UIViewContentModeScaleAspectFill;
        _contentImgView.image = [UIImage imageNamed:@"mine_default_header"];
    }
    return _contentImgView;
}

-(UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.expandEdgeInsets = UIEdgeInsetsMake(0, 5, 5, 0);
        _deleteBtn.hidden = YES;
        [_deleteBtn setImage:ImageWithName(@"icon_photo_delete") forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}




@end
